# _*_ coding:utf8 _*_
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User

# Create your models here.


class Hosts(models.Model):
    name = models.CharField(max_length=100, unique=True)
    ip_addr = models.GenericIPAddressField(max_length=100, unique=True)
    system_type_choices = (
        ('linux', 'Linux/Unix'),
        ('windows', 'Windows'),
    )
    system_type = models.CharField(choices=system_type_choices, default='linux', max_length=200)
    idc = models.ForeignKey('IDC')
    port = models.IntegerField(default=33890)
    enabled = models.BooleanField(default=True)
    memo = models.CharField(max_length=200, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return "%s/(%s)" % (self.name, self.ip_addr)

    class Meta:
        verbose_name = u'主机'
        verbose_name_plural = u'主机'


class IDC(models.Model):
    name = models.CharField(max_length=100)
    memo = models.CharField(max_length=200, blank=True, null=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'IDC商'
        verbose_name_plural = u'IDC商'


class HostUser(models.Model):
    auth_type_choices = (
        ('password', 'Password'),
        ('ssh-key', 'SSH-Key')
    )
    username = models.CharField(max_length=100)
    password = models.CharField(max_length=100)
    auth_type = models.CharField(choices=auth_type_choices, default='password', max_length=200)
    memo = models.CharField(max_length=200, blank=True, null=True)

    def __unicode__(self):
        return "%s/(%s)" % (self.username, self.auth_type)

    class Meta:
        verbose_name = u'主机用户'
        verbose_name_plural = u'主机用户'


class Department(models.Model):
    name = models.CharField(max_length=100)
    memo = models.CharField(max_length=200, blank=True, null=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'部门'
        verbose_name_plural = u'部门'


class HostGroups(models.Model):
    name = models.CharField(max_length=100)
    memo = models.CharField(max_length=200, blank=True, null=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'主机组'
        verbose_name_plural = u'主机组'


class UserProfile(models.Model):
    user = models.OneToOneField(User)
    name = models.CharField(max_length=100)
    department = models.ForeignKey('Department')
    head_img = models.ImageField(upload_to="static/img/nav", default="static/img/avatar.png")
    host_group = models.ManyToManyField('HostGroups', verbose_name=u"授权主机组", blank=True)
    bind_hosts = models.ManyToManyField('BindHosts',  verbose_name=u'授权主机', blank=True)
    game = models.ManyToManyField('GameClusteer', verbose_name=u'授权游戏服集群', blank=True)
    vaild_begin_time = models.DateTimeField(auto_now_add=True)
    vaild_end_time = models.DateTimeField()

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'用户配置文件'
        verbose_name_plural = u'用户配置文件'


class BindHosts(models.Model):
    host = models.ForeignKey('Hosts')
    host_user = models.ForeignKey('HostUser')
    host_group = models.ManyToManyField('HostGroups')
    enabled = models.BooleanField(default=True)

    def __unicode__(self):
        return "%s:%s" % (self.host.name, self.host_user.username)

    class Meta:
        unique_together = ("host", "host_user")
        verbose_name = u'主机与远程用户绑定'
        verbose_name_plural = u'主机远程与用户绑定'


class Games(models.Model):
    type_choices = (
        (u'游戏服', u'游戏服'),
        (u'天道功能服', u'天道功能服'),
        (u'主节点', u'主节点'),
        (u'竞技场', u'竞技场'),
    )
    name = models.CharField(max_length=200)
    name_remark = models.CharField(max_length=200)
    sid = models.CharField(max_length=100)
    client_dir = models.ForeignKey('GameCdnClient')
    server_ip = models.GenericIPAddressField()
    server_domain = models.CharField(max_length=100, default='qqs1.sg.du175.com')
    game_sport = models.CharField(max_length=100)
    res_domain = models.ForeignKey('GameCdnDomain', default=1)
    status = models.BooleanField(default=True)
    type = models.CharField(choices=type_choices, default=u'游戏服', max_length=200)
    platform = models.ForeignKey('GamePlatform', max_length=100)
    cluster = models.ForeignKey('GameClusteer', max_length=100, default=1)
    memo = models.CharField(max_length=200, blank=True, null=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'游戏服'
        verbose_name_plural = u'游戏服'


class GamePlatform(models.Model):
    name = models.CharField(max_length=200)
    memo = models.CharField(max_length=100, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'游戏平台'
        verbose_name_plural = u'游戏平台'


class GameClusteer(models.Model):
    name = models.CharField(max_length=200)
    memo = models.CharField(max_length=200, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'游戏服集群'
        verbose_name_plural = u'游戏服集群'


class GameCdnClient(models.Model):
    name = models.CharField(max_length=100)
    memo = models.CharField(max_length=200, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'CDN客户端'
        verbose_name_plural = u'CDN客户端'


class GameCdnDomain(models.Model):
    name = models.CharField(max_length=100)
    memo = models.CharField(max_length=200, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'CDN域名'
        verbose_name_plural = u'CDN域名'